package com.twu.biblioteca;

import com.twu.biblioteca.command.*;
import com.twu.biblioteca.menu.Menu;
import com.twu.biblioteca.menu.MenuItem;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class BibliotecaApp {

    private PrintWriter printWriter;
    private InputStreamReader inputReader;
    private Library lib;
    private UsersManager userManager;
    private Menu menu;

    public BibliotecaApp(Library lib, UsersManager userManager) {
        this.lib = lib;
        this.userManager = userManager;
        this.printWriter = new PrintWriter(System.out);
        this.inputReader = new InputStreamReader(System.in);

        setUpMenu();
    }

    public void setPrintWriter(PrintWriter printWriter) {
        this.printWriter = printWriter;
    }

    public void setInputReader(InputStreamReader inputReader) {
        this.inputReader = inputReader;
    }

    public static void main(String[] args) {
        BibliotecaApp app = new BibliotecaApp(Library.createLibrary(), UsersManager.getUserManager());
        app.run();
    }
    public void run(){
        printWelcomeMessage();
        printMenuOptions();
        showPrompt();
        String option = readUserInput();
        while(!option.equals("exit")){
            if(option.equals("0")){
                printMenuOptions();
            }else{
                String result = menu.executeMenuItem(option);
                showResult(result);
            }
            showPrompt();
            option = readUserInput();
        }
    }

    private void showPrompt() {
        printWriter.print("Input option Id > ");
        printWriter.flush();
    }

    private void showResult(String result) {
        printWriter.println(result);
    }

    private void setUpMenu() {
        menu = new Menu();
        Command viewAllBookCmd = new ViewAllBooksCommand(lib);
        Command viewAllMovieCmd = new ViewAllMoviesCommand(lib);
        Command reserveBookCmd = new ReserveBookCommand(inputReader, printWriter, lib);
        Command checkMemberShipCmd = new CheckMemberShipCommand(userManager);

        userManager.needAuthentication(reserveBookCmd);

        menu.addMenuItem(new MenuItem("view all books", new CommandProxy(viewAllBookCmd, userManager)));
        menu.addMenuItem(new MenuItem("view all movies", new CommandProxy(viewAllMovieCmd, userManager)));
        menu.addMenuItem(new MenuItem("reserve book", new CommandProxy(reserveBookCmd, userManager)));
        menu.addMenuItem(new MenuItem("check membership", new CommandProxy(checkMemberShipCmd, userManager)));
    }

    public void printWelcomeMessage() {
        printWriter.println("Welcome to Biblioteca");
    }

    public void printMenuOptions() {
        printWriter.println("Available options: ");
        menu.show(printWriter);
        printWriter.println("Enter 0 to show this menu");
    }

    public String readUserInput() {
        BufferedReader br = new BufferedReader(this.inputReader);

        String userInput = "";
        try {
            userInput = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if(userInput==null)
            return "";

        return userInput;
    }
}

